package com.isoftstone.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

import com.isoftstone.pojo.Stipend;
import com.isoftstone.service.StipendService;

@Controller
public class StipendController {

	@Autowired
	private StipendService stipendService;

	public StipendService getStipendService() {
		return stipendService;
	}

	public void setStipendService(StipendService stipendService) {
		this.stipendService = stipendService;
	}

	@RequestMapping("liststipend")
	public String list(Model model) {
		List<Stipend> stipends = stipendService.selectAll();
		model.addAttribute("list", stipends);
		return "liststipend";
	}

	@RequestMapping("toupdatestipend")
	public String toModifyStipend(Model model, WebRequest request) {
		int id=Integer.valueOf( request.getParameter("id").trim());
        Stipend stipend=stipendService.getStipend(id);
        model.addAttribute("stipend", stipend);
		return "updatestipend";
	}

	@RequestMapping("todeletestipend")
	public String toDeleteStipend(Model model, WebRequest request) {
		int id=Integer.valueOf( request.getParameter("id").trim());
         stipendService.delete(id);
        List<Stipend> stipends = stipendService.selectAll();
		model.addAttribute("list", stipends);
		return "liststipend";
	}
	
	@RequestMapping(path = "updatestipend", method = RequestMethod.POST)
	public String ModifyStipend(Model model, Stipend stipend) {
		stipend.computeTotalize();
		stipendService.updateStipend(stipend);
		List<Stipend> stipends = stipendService.selectAll();
		model.addAttribute("list", stipends);

		return "liststipend";
	}
	
	@RequestMapping(path = "addstipend", method = RequestMethod.POST)
	public String insertStipend(Model model,String name,String granttime,String basic, String eat, String house, String duty, String scot, String punishment, String other) {
		Stipend stipend =new Stipend();
		stipend.setName(name);
		stipend.setGranttime(granttime);
		stipend.setBasic(Float.parseFloat(basic));
		stipend.setEat(Float.parseFloat(eat==""?"0":eat));
		stipend.setHouse(Float.parseFloat(house==""?"0":house));
		stipend.setDuty(Float.parseFloat(duty==""?"0":duty));
		stipend.setScot(Float.parseFloat(scot==""?"0":scot));
		stipend.setPunishment(Float.parseFloat(punishment==""?"0":punishment));
		stipend.setOther(Float.parseFloat(other==""?"0":other));
		stipend.computeTotalize();
		
		
		stipendService.insertStipend(stipend);
		
		return "addstipend";
	}
	
	
	@RequestMapping("toinsertstipend")
	public String toInsertStipend() {
		return "addstipend";
	}
	
}
